﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMI_TS_CABINET : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;
        private int iGridView2 = 0;
        private int detailindex = -1;
        DataTable myDetail;

        public FMI_TS_CABINET()
        {
            InitializeComponent();
        }

        //数据保存成功后执行操作
        private void doProc()
        {
            string sql = "exec proc_biz_bizcenter 'i_ts_cabinet','','" + uifequipmentbarcode.Text + "',2,'" + sUser + "'";
            StaticClass.myDB.doexec(sql);
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = "select b.fpoint_name,c.fdictname,a.*"
              + "  from i_ts_cabinet a"
              + "  inner join a_ts_point b on b.fpoint_no=a.fpoint_no"
              + "  inner join s_ts_dict c on c.fdicttype = 3001 and c.fdictno = a.fstorageflag"
              + "  where 1 = 1";
            DataRowView drv = (DataRowView)uiCBfstorageflag.SelectedItem;
            if ((drv != null)&&(uiCBfstorageflag.SelectedIndex>0))
            {
                string sfstorageflag = drv.Row["fDictNo"].ToString();
                SQL = SQL + " and c.fdictno='" + sfstorageflag + "'";
            }
            DataRowView drv2 = (DataRowView)cbfpoint_no.SelectedItem;
            if ((drv2 != null)&&(cbfpoint_no.SelectedIndex>0))
            {
                string sfpoint_no = drv2.Row["fpoint_no"].ToString();
                SQL = SQL + " and a.fpoint_no='" + sfpoint_no + "'";
            }
            if (qifequipmentbarcode.Text != "")
            {
                SQL = SQL + " and ((fequipmentbarcode like '%" + qifequipmentbarcode.Text + "%')"
                 + " or (fequipmenttag like '%" + qifequipmentbarcode.Text + "%'))";
            }
            SQL = SQL + "  order by b.fpoint_no,c.fdictname,a.fequipmentbarcode";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
            
        }

        private void FMI_TS_CABINET_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "Import_i_ts_cabinet";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];

            //按钮控制
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=3001");
            uifstorageflag.DataSource = myTs_Dict;
            uifstorageflag.DisplayMember = "fDictName";
            uifstorageflag.ValueMember = "fDictNo";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fDictName"] = "请选择";
            dr1["fDictNo"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            uiCBfstorageflag.DataSource = myTs_Dict;
            uiCBfstorageflag.DisplayMember = "fDictName";
            uiCBfstorageflag.ValueMember = "fDictNo";
            uiCBfstorageflag.SelectedIndex = 0;

            DataTable myTs_Point = myCache.doGetDict("A_TS_POINT", "fpoint_flag=0");
            uifpoint_no.DataSource = myTs_Point;
            uifpoint_no.DisplayMember = "fpoint_name";
            uifpoint_no.ValueMember = "fpoint_no";

            DataRow dr2 = myTs_Point.NewRow();
            dr2["fpoint_name"] = "请选择";
            dr2["fpoint_no"] = "0";
            myTs_Point.Rows.InsertAt(dr2, 0);
            cbfpoint_no.DataSource = myTs_Point;
            cbfpoint_no.DisplayMember = "fpoint_name";
            cbfpoint_no.ValueMember = "fpoint_no";
            cbfpoint_no.SelectedIndex = 0;

            DataTable myTs_Dict2 = myCache.doGetDict("S_TS_DICT", "fDictType=1003");
            uiflabeltype.DataSource = myTs_Dict2;
            uiflabeltype.DisplayMember = "fDictName";
            uiflabeltype.ValueMember = "fDictNo";

            DataTable myTs_stock = myCache.doGetDict("I_TS_STOCK", "");
            uifstockno.DataSource = myTs_stock;
            uifstockno.DisplayMember = "fstockname";
            uifstockno.ValueMember = "fstockno";

            DataTable myTs_State = myCache.doGetDict("S_TS_EQUIPMENT_STATE", "");
            uifstate.DataSource = myTs_State;
            uifstate.DisplayMember = "fstatename";
            uifstate.ValueMember = "fstateno";

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifequipmentbarcode.Text = "";
            uifequipmenttag.Text = "";
            uifmacno.Text = "";
            uifpoint_size.Text = "";
            uifvideourl.Text = "";
            uifip.Text = "";
            uifport.Text = "5000";
            uiflamp1.Text = "";
            uiflamp2.Text = "";
            uiflamp3.Text = "";

            uifstorageflag.SelectedIndex = 0;
            uiflabeltype.SelectedIndex = 0;
            uifstate.SelectedIndex = 1;
            uifpoint_no.SelectedValue = "";
            uifstockno.SelectedValue = "";

            DataTable myNo = StaticClass.myDB.query("exec fun_barcode 2");
            if ((myNo != null) && (myNo.Rows.Count > 0))
            {
                uifequipmentbarcode.Text = myNo.Rows[0][0].ToString();
                uifmacno.Text = myNo.Rows[0][0].ToString();
            }
            sType = "A";
            doLoadData();
            uifequipmentbarcode.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifequipmentbarcode.Text = uiDGView["fequipmentbarcode", rowindex].Value.ToString();
                uifequipmenttag.Text = uiDGView["fequipmenttag", rowindex].Value.ToString();
                uifmacno.Text = uiDGView["fmacno", rowindex].Value.ToString();
                uifip.Text = uiDGView["fip", rowindex].Value.ToString();
                uifport.Text = uiDGView["fport", rowindex].Value.ToString();
                uiflamp1.Text = uiDGView["flamp1", rowindex].Value.ToString();
                uiflamp2.Text = uiDGView["flamp2", rowindex].Value.ToString();
                uiflamp3.Text = uiDGView["flamp3", rowindex].Value.ToString();
                uifvideourl.Text = uiDGView["fvideourl", rowindex].Value.ToString();
                uifpoint_size.Text = uiDGView["fpoint_size", rowindex].Value.ToString();

                int i = 0;
                string sstatus = uiDGView["fstate", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifstate.Items)
                {
                    string id = dr["fstateno"].ToString();
                    if (id.Equals(sstatus))
                    {
                        uifstate.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfpoint_no = uiDGView["fpoint_no", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifpoint_no.Items)
                {
                    string id = dr["fpoint_no"].ToString();
                    if (id.Equals(sfpoint_no))
                    {
                        uifpoint_no.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfstockno = uiDGView["fstockno", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifstockno.Items)
                {
                    string id = dr["fstockno"].ToString();
                    if (id.Equals(sfstockno))
                    {
                        uifstockno.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sflabeltype = uiDGView["flabeltype", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uiflabeltype.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sflabeltype))
                    {
                        uiflabeltype.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfstorageflag = uiDGView["fstorageflag", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifstorageflag.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sfstorageflag))
                    {
                        uifstorageflag.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }

            //载入明细
            string sql = "select a.fseq,a.fboxbarcode,b.fmacno,b.fequipmenttag fboxname"
              + " from i_ts_cabbox a"
              + " left join i_ts_warehouse b on a.fboxbarcode = b.fequipmentbarcode"
              + " where 1 = 1"
              + " and a.fequipmentbarcode='" + uifequipmentbarcode.Text + "'"
              + " order by fseq";
            myDetail = StaticClass.myDB.query(sql);
            uiDetail.DataSource = myDetail;

            //表格列显示统一控制
            if (iGridView2 == 0)
            {
                myCache.doSetGridVIew(sMenu + "1", sUser, uiDetail);
                iGridView2 = iGridView2 + 1;
            }

            uibtn1.Visible = true;
            uibtn2.Visible = true;
            //uiPanel1.Height = 300;

            if (sType.Equals("V"))
            {
                uibtn1.Visible = false;
                uibtn2.Visible = false;
                //uiPanel1.Height = 260;
            }
            detailindex = -1;
        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifequipmentbarcode.ReadOnly = true;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fequipmenttag", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fequipmentbarcode", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'i_ts_cabinet','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();
            
            base.OnSys10();
        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifequipmentbarcode, "请输入固定资产码")
                   && CheckEmpty(uifequipmenttag, "请输入设备名称")
                   && CheckEmpty(uifmacno, "请输入设备序列号")
                   && CheckEmpty(uifstorageflag, "请选择存储分类")
                   && CheckEmpty(uifpoint_no, "请选择存放地点")
                   && CheckEmpty(uifstate, "请选择状态")
                   && CheckEmpty(uifstockno, "请选择仓库");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            DataRowView drv1 = (DataRowView)uifstate.SelectedItem;
            string sstatus = drv1.Row["fstateno"].ToString();

            DataRowView drv2 = (DataRowView)uifpoint_no.SelectedItem;
            string sfpoint_no = drv2.Row["fpoint_no"].ToString();

            DataRowView drv3 = (DataRowView)uifstockno.SelectedItem;
            string sfstockno = drv3.Row["fstockno"].ToString();

            DataRowView drv4 = (DataRowView)uiflabeltype.SelectedItem;
            string sflabeltype = drv4.Row["fDictNo"].ToString();

            DataRowView drv5 = (DataRowView)uifstorageflag.SelectedItem;
            string sfstorageflag = drv5.Row["fDictNo"].ToString();

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        i_ts_cabinet myData = context.i_ts_cabinet.Find(uifequipmentbarcode.Text);
                        if (myData is null)
                        {
                            i_ts_cabinet myData2 = new i_ts_cabinet();
                            myData2.fequipmentbarcode = uifequipmentbarcode.Text;
                            myData2.feuipmentid = 17;
                            myData2.fequipmenttag = uifequipmenttag.Text;
                            myData2.fmacno = uifmacno.Text;
                            myData2.fpoint_size = uifpoint_size.Text;
                            myData2.fvideourl = uifvideourl.Text;
                            myData2.fip = uifip.Text;
                            myData2.fport = uifport.Text;
                            myData2.flamp1 = uiflamp1.Text;
                            myData2.flamp2 = uiflamp2.Text;
                            myData2.flamp3 = uiflamp3.Text;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fsite_no = sSite_No;
                            myData2.fstate = int.Parse(sstatus);
                            myData2.fpoint_no = sfpoint_no;
                            myData2.fstockno = sfstockno;
                            myData2.fstorageflag = sfstorageflag;
                            myData2.flabeltype = int.Parse(sflabeltype);
                            context.i_ts_cabinet.Add(myData2);
                            context.SaveChanges();

                            doSetDetail();
                            doProc();
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    i_ts_cabinet myts_box = context.i_ts_cabinet.Find(uifequipmentbarcode.Text);

                    myts_box.fequipmenttag = uifequipmenttag.Text;
                    myts_box.fmacno = uifmacno.Text;
                    myts_box.fpoint_size = uifpoint_size.Text;
                    myts_box.fvideourl = uifvideourl.Text;
                    myts_box.fip = uifip.Text;
                    myts_box.fport = uifport.Text;
                    myts_box.flamp1 = uiflamp1.Text;
                    myts_box.flamp2 = uiflamp2.Text;
                    myts_box.flamp3 = uiflamp3.Text;
                    myts_box.foperator = sUser;
                    myts_box.fupddate = DateTime.Now;
                    myts_box.fstate = int.Parse(sstatus);
                    myts_box.fpoint_no = sfpoint_no;
                    myts_box.fstorageflag = sfstorageflag;
                    myts_box.fstockno = sfstockno;
                    myts_box.flabeltype = int.Parse(sflabeltype);
                    int result = context.SaveChanges();
                    doSetDetail();
                    doProc();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        private void uiDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            detailindex = e.RowIndex;
        }

        private void uiDetail_DoubleClick(object sender, EventArgs e)
        {
            string iMenu = sMenu + "1";
            OnGridDbClick(iMenu, uiDetail);
        }

        private void uibtn1_Click(object sender, EventArgs e)
        {
            //添加明细 a.fseq,a.fequipmentbarcode,b.fmacno,b.fequipmenttag fcatname
            int i = 1;
            if (myDetail.Rows.Count > 0)
            {
                i = int.Parse(myDetail.Rows[myDetail.Rows.Count - 1]["fseq"].ToString()) + 1;
            }

            //弹窗选择
            DataRowView drv5 = (DataRowView)uifstorageflag.SelectedItem;
            if (drv5 == null)
            {
                UIMessageTip.ShowOk("请选择存储分类");
                return;
            }
            string sfstorageflag = drv5.Row["fDictNo"].ToString();


            DataRowView drv3 = (DataRowView)uifpoint_no.SelectedItem;
            if (drv3 == null)
            {
                UIMessageTip.ShowOk("请选择存放地点");
                return;
            }
            string sfpoint_no = drv3.Row["fpoint_no"].ToString();

            
            string sql = "select fequipmentbarcode,fmacno,fequipmenttag,fpoint_no,fsite_no "
                + " from i_ts_warehouse a"
                + " where fstate=1 and fstorageflag='" + sfstorageflag + "'"
                + " and a.fpoint_no='"+sfpoint_no+"'"
                + " and not exists(select 1 from i_ts_cabbox s where s.fboxbarcode=a.fequipmentbarcode)";

            string DATA = doPOPWINDOS("储物仓查询"
                , "fequipmentbarcode,固定资产码,121;fmacno,设备序列号,121;fequipmenttag,设备名称,161;fpoint_no,存放地点,121"
                , sql
                , " order by fequipmentbarcode"
                ,1);
            Console.WriteLine("当前返回:" + DATA);

            if ((DATA is null) || (DATA.Length == 0))
            {
                return;
            }
            string[] strList = DATA.Split(';');

            for (int j = 0; j < strList.Length; j++)
            {
                string[] strArray = strList[j].Split(',');

                if (strArray.Length > 1)
                {
                    //存在不保存
                    DataRow[] foundRow = myDetail.Select("fboxbarcode = '" + strArray[0] + "'");
                    if (foundRow.Length == 0)
                    {
                        //不存在新增
                        DataRow dr = myDetail.NewRow();
                        dr["fseq"] = i.ToString(); ;
                        dr["fboxbarcode"] = strArray[0];
                        dr["fmacno"] = strArray[1];
                        dr["fboxname"] = strArray[2];
                        myDetail.Rows.InsertAt(dr, myDetail.Rows.Count);

                        i = i + 1;
                    }
                }
            }
            
        }

        private void uibtn2_Click(object sender, EventArgs e)
        {
            //删除明细
            Console.WriteLine("行:" + detailindex.ToString());
            if (detailindex == -1)
            {
                UIMessageTip.ShowOk("请先选择要删除的记录");
                return;
            }
            if (this.ShowAskDialog("确定删除吗?"))
            {
                string iseq = myDetail.Rows[detailindex]["fseq"].ToString();
                DataRow[] foundRow = myDetail.Select("fseq = " + iseq, "");
                foreach (DataRow row in foundRow)
                {
                    myDetail.Rows.Remove(row);
                }
            }
        }

        //保存明细
        private Boolean doSetDetail()
        {
            bool IsOk = false;
            StaticClass.myDB.SQLList.Clear();
            string sql = "delete from i_ts_cabbox where fequipmentbarcode='" + uifequipmentbarcode.Text + "'";
            StaticClass.myDB.SQLList.Add(sql);

            for (int i = 0; i < myDetail.Rows.Count; i++)
            {
                sql = "insert into i_ts_cabbox(fequipmentbarcode,fboxbarcode,fseq,foperator,fupddate)"
                    + " values('" + uifequipmentbarcode.Text + "'"
                    + ",'" + myDetail.Rows[i]["fboxbarcode"].ToString() + "'"
                    + "," + myDetail.Rows[i]["fseq"].ToString()
                    + ",'" + sUser + "'"
                    + ",getdate())";
                StaticClass.myDB.SQLList.Add(sql);
            }
            string sError = string.Empty;
            StaticClass.myDB.doSave(out IsOk, out sError);
            return IsOk;
        }

        private void cbfpoint_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void uiCBfstorageflag_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
